package com.leetcode.LC_MED;

public class LC92 {
    //https://www.bilibili.com/video/BV1wA411b7qZ?p=20
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode pre = null;
        ListNode curr = head;
        ListNode next = head;

        for (int i = 1; i < left; i++) {
            pre = curr;
            curr = curr.next;
        }
        ListNode pre2 = pre;
        ListNode curr2 = curr;

        for (int i = left; i <= right; i++) {
            next = curr.next;
            curr.next = pre;
            pre = curr;
            curr = next;
        }
        if (pre2 != null) {
            pre2.next = pre;
        }else {
            head=pre;
        }
        curr2.next = curr;
        return head;
    }
}
